package basecamp.topo.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import basecamp.topo.client.TopoService;
import basecamp.topo.shared.FaceID;
import basecamp.topo.shared.FieldVerifier;


import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class TopoServiceImpl extends RemoteServiceServlet implements
		TopoService {

	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Name must be at least 4 characters long");
		}

		String serverInfo = getServletContext().getServerInfo();
		String userAgent = getThreadLocalRequest().getHeader("User-Agent");

		// Escape data from the client to avoid cross-site script vulnerabilities.
		input = escapeHtml(input);
		userAgent = escapeHtml(userAgent);

		return "Hello, " + input + "!<br><br>I am running " + serverInfo
				+ ".<br><br>It looks like you are using:<br>" + userAgent;
	}

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}

	
	@Override
	public void addFace(String name, String svg)
			throws IllegalArgumentException {
		mFaces.put(mFaces.size() + 1, new RockFace(name, svg));
		
	}

	FaceID buba(FaceID f){
		return null;
	}
	HashMap<Integer, RockFace> mFaces = new HashMap<Integer, RockFace>();
	public TopoServiceImpl(){
		mFaces.put(1, new RockFace("Psiklatka", "<svg width=\"640\" height=\"480\" xmlns=\"http://www.w3.org/2000/svg\"><rect height=\"179\" width=\"220\" y=\"120\" x=\"166\" stroke-width=\"5\" stroke=\"#000000\" fill=\"#FF0000\"/></svg>"));
		mFaces.put(2, new RockFace("Zaklety mur", "<svg width=\"640\" height=\"480\" xmlns=\"http://www.w3.org/2000/svg\"><rect height=\"179\" width=\"220\" y=\"120\" x=\"166\" stroke-width=\"53\" stroke=\"#000000\" fill=\"#FFFF00\"/></svg>"));
		mFaces.put(3, new RockFace("Witkowe", "<svg width=\"640\" height=\"480\" xmlns=\"http://www.w3.org/2000/svg\"><rect height=\"179\" width=\"220\" y=\"120\" x=\"166\" stroke-width=\"5\" stroke=\"#000000\" fill=\"#FF0000\"/></svg>"));
		
	}
	@Override
	public List<FaceID> getFaces() throws IllegalArgumentException {
		List<FaceID> ret = new ArrayList<FaceID>();
		for(Entry<Integer, RockFace> i : mFaces.entrySet()){
			ret.add(new FaceID(i.getValue().mName, i.getKey().intValue()));
		}
		return ret;
	}

	@Override
	public String getFaceSVG(int id) throws IllegalArgumentException {
		return mFaces.get(id).mSvg;
		
	}

//	@Override
//	public RockFace getFace(int id) throws IllegalArgumentException {
//		// TODO Auto-generated method stub
//		return null;
//	}
}
